import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  HttpStatus,
  HttpCode,
  NotFoundException,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam, ApiBody, ApiResponse } from '@nestjs/swagger';
import { ClassService } from './class.service';
import { CreateClassDto } from './dto/create-class.dto';
import { UpdateClassDto } from './dto/update-class.dto';
import { QueryClassDto } from './dto/query-class.dto';
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator';
import { definePermission, Perm } from '~/modules/admin/auth/decorators/permission.decorator';
import { ClassAttendanceStatisticsDto } from './dto/class-attendance-statistics.dto';
import { AddStudentsDto } from './dto/add-students.dto';

export const permissions = definePermission('manage:class', {
  LIST: 'list',
  CREATE: 'create',
  READ: 'read',
  UPDATE: 'update',
  DELETE: 'delete',
  ATTENDANCE_STATISTICS: 'attendance_statistics',
  ADD_STUDENTS: 'add_students',
} as const);

@ApiTags('Manage - 班级管理')
@ApiSecurityAuth()
@Controller('admin/manage/class')
export class ClassController {
  constructor(private readonly classService: ClassService) {}

  @Post()
  @ApiOperation({ summary: '创建班级' })
  @HttpCode(HttpStatus.CREATED)
  @Perm(permissions.CREATE)
  create(@Body() createClassDto: CreateClassDto) {
    return this.classService.create(createClassDto);
  }

  @Get()
  @ApiOperation({ summary: '获取班级列表' })
  @Perm(permissions.LIST)
  findAll(@Query() queryDto: QueryClassDto) {
    return this.classService.findAll(queryDto);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取班级详情' })
  @Perm(permissions.READ)
  findOne(@Param('id') id: string) {
    return this.classService.findOne(id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新班级' })
  @Perm(permissions.UPDATE)
  update(@Param('id') id: string, @Body() updateClassDto: UpdateClassDto) {
    return this.classService.update(id, updateClassDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除班级' })
  @HttpCode(HttpStatus.NO_CONTENT)
  @Perm(permissions.DELETE)
  remove(@Param('id') id: string) {
    return this.classService.remove(id);
  }

  @ApiOperation({ summary: '获取班级考勤数据统计', description: '获取指定班级的考勤统计数据，包括总体、季度、月度和周度数据' })
  @ApiParam({ name: 'id', description: '班级ID' })
  @ApiResponse({ 
    status: 200, 
    description: '获取成功',
    type: ClassAttendanceStatisticsDto
  })
  @Perm(permissions.ATTENDANCE_STATISTICS)
  @Get(':id/attendance-statistics')
  async getClassAttendanceStatistics(@Param('id') id: string) {
    return this.classService.getClassAttendanceStatistics(id);
  }

  @ApiOperation({ summary: '添加学生到班级', description: '将一个或多个学生添加到指定班级' })
  @ApiParam({ name: 'id', description: '班级ID' })
  @ApiBody({ type: AddStudentsDto })
  @ApiResponse({ status: 201, description: '添加成功' })
  @HttpCode(HttpStatus.CREATED)
  @Perm(permissions.ADD_STUDENTS)
  @Post(':id/students')
  addStudentsToClass(@Param('id') id: string, @Body() addStudentsDto: AddStudentsDto) {
    return this.classService.addStudentsToClass(id, addStudentsDto);
  }
}
